{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "O0vh6E2uASWK"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "# Load the CIFAR-10 dataset\n",
        "(x_train, _), (x_test, _) = tf.keras.datasets.cifar10.load_data()\n",
        "\n",
        "# Preprocess the data (normalize, reshape)\n",
        "x_train = x_train.astype('float32') / 255.\n",
        "x_test = x_test.astype('float32') / 255.\n",
        "x_train = x_train.reshape((len(x_train), -1))\n",
        "x_test = x_test.reshape((len(x_test), -1))\n",
        "\n",
        "# Define and train the autoencoder model\n",
        "latent_dim = 2  # Set the desired dimensionality of the latent space\n",
        "input_dim = x_train.shape[1]\n",
        "\n",
        "# Define the encoder model\n",
        "encoder = tf.keras.models.Sequential([\n",
        "  tf.keras.layers.Dense(512, activation='relu', input_shape=(input_dim,)),\n",
        "  tf.keras.layers.Dense(256, activation='relu'),\n",
        "  tf.keras.layers.Dense(latent_dim, activation='linear')\n",
        "])\n",
        "\n",
        "# Define the decoder model\n",
        "decoder = tf.keras.models.Sequential([\n",
        "  tf.keras.layers.Dense(256, activation='relu', input_shape=(latent_dim,)),\n",
        "  tf.keras.layers.Dense(512, activation='relu'),\n",
        "  tf.keras.layers.Dense(input_dim, activation='sigmoid')\n",
        "])\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Combine the encoder and decoder into an autoencoder model\n",
        "autoencoder = tf.keras.models.Sequential([encoder, decoder])\n",
        "\n",
        "# Compile and train the autoencoder\n",
        "autoencoder.compile(optimizer='adam', loss='binary_crossentropy')\n",
        "autoencoder.fit(x_train, x_train, epochs=10, batch_size=128, validation_data=(x_test, x_test))\n",
        "\n",
        "# Get the latent space representation of the test data\n",
        "latent_space = encoder.predict(x_test)\n",
        "\n",
        "# Visualize the latent space\n",
        "plt.scatter(latent_space[:, 0], latent_space[:, 1], c='b', cmap='tab10')\n",
        "plt.colorbar()\n",
        "plt.title('Latent Space Visualization')\n",
        "plt.xlabel('Latent Dimension 1')\n",
        "plt.ylabel('Latent Dimension 2')\n",
        "plt.show()\n"
      ],
      "metadata": {
        "id": "0Drr5KblAjzp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "# Load CIFAR-10 dataset\n",
        "(train_images, _), (_, _) = tf.keras.datasets.cifar10.load_data()\n",
        "train_images = (train_images - 127.5) / 127.5  # Normalize the images to [-1, 1]\n",
        "\n",
        "# Generator model\n",
        "generator = tf.keras.models.Sequential([\n",
        "  tf.keras.layers.Dense(256, input_dim=100, activation='relu'),\n",
        "  tf.keras.layers.Dense(512, activation='relu'),\n",
        "  tf.keras.layers.Dense(1024, activation='relu'),\n",
        "  tf.keras.layers.Dense(3072, activation='tanh'),\n",
        "  tf.keras.layers.Reshape((32, 32, 3))\n",
        "])\n",
        "\n",
        "# Generate random latent vectors\n",
        "latent_vectors = tf.random.normal((100, 100))\n",
        "\n",
        "# Generate images from latent vectors\n",
        "generated_images = generator.predict(latent_vectors)\n",
        "\n",
        "# Visualize the generated images\n",
        "fig, axes = plt.subplots(10, 10, figsize=(10, 10))\n",
        "  for i, ax in enumerate(axes.flat):\n",
        "    ax.imshow((generated_images[i] + 1) / 2)  # Rescale to [0, 1]\n",
        "  ax.axis('off')\n",
        "  plt.show()\n"
      ],
      "metadata": {
        "id": "FQ6kt2OiAwsC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "from tensorflow.keras.datasets import mnist\n",
        "import numpy as np\n",
        "\n",
        "# Load MNIST dataset\n",
        "(x_train, _), (x_test, _) = mnist.load_data()\n",
        "\n",
        "# Normalize and reshape input data\n",
        "x_train = x_train.astype('float32') / 255.0\n",
        "x_test = x_test.astype('float32') / 255.0\n",
        "x_train = np.expand_dims(x_train, axis=-1)\n",
        "x_test = np.expand_dims(x_test, axis=-1)\n",
        "\n",
        "# Define the encoder model\n",
        "encoder = tf.keras.Sequential([\n",
        "tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same', input_shape=(28, 28, 1)),\n",
        "tf.keras.layers.MaxPooling2D((2, 2), padding='same'),\n",
        "tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'),\n",
        "tf.keras.layers.MaxPooling2D((2, 2), padding='same'),\n",
        "tf.keras.layers.Conv2D(4, (3, 3), activation='relu', padding='same'),\n",
        "tf.keras.layers.MaxPooling2D((2, 2), padding='same')\n",
        "])\n",
        "\n",
        "# Define the decoder model\n",
        "decoder = tf.keras.Sequential([\n",
        "  tf.keras.layers.Conv2D(4, (3, 3), activation='relu', padding='same'),\n",
        "  tf.keras.layers.UpSampling2D((2, 2)),\n",
        "  tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same'),\n",
        "  tf.keras.layers.UpSampling2D((2, 2)),\n",
        "  tf.keras.layers.Conv2D(16, (3, 3), activation='relu'),\n",
        "  tf.keras.layers.UpSampling2D((2, 2)),\n",
        "  tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')\n",
        "])\n",
        "\n",
        "# Combine the encoder and decoder\n",
        "autoencoder = tf.keras.Sequential([encoder, decoder])\n",
        "\n",
        "# Compile the model\n",
        "autoencoder.compile(optimizer='adam', loss='binary_crossentropy')\n",
        "\n",
        "# Train the model\n",
        "autoencoder.fit(x_train, x_train, epochs=10, batch_size=128, validation_data=(x_test, x_test))\n",
        "\n",
        "# Generate colored output for test images\n",
        "colored_images = autoencoder.predict(x_test)\n",
        "\n",
        "# Visualize the original and reconstructed images\n",
        "import matplotlib.pyplot as plt\n",
        "\n",
        "n = 10  # Number of images to visualize\n",
        "plt.figure(figsize=(20, 4))\n",
        "for i in range(n):\n",
        "    # Display original MNIST image\n",
        "    ax = plt.subplot(2, n, i + 1)\n",
        "plt.imshow(x_test[i].reshape(28, 28), cmap='gray')\n",
        "plt.title('Original')\n",
        "plt.axis('off')\n",
        "\n",
        "    # Display reconstructed colorized image\n",
        "    ax = plt.subplot(2, n, i + 1 + n)\n",
        "plt.imshow(colored_images[i].reshape(28, 28), cmap='jet')\n",
        "plt.title('Colorized')\n",
        "plt.axis('off')\n",
        "\n",
        "plt.show()\n"
      ],
      "metadata": {
        "id": "SaOrF8PNBojg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "from tensorflow.keras.layers import Input, Dense\n",
        "from tensorflow.keras.models import Model\n",
        "\n",
        "# Load and preprocess your dataset\n",
        "dataset = …  # Your dataset\n",
        "# Perform any necessary preprocessing steps\n",
        "\n",
        "# Define input shape\n",
        "input_shape = dataset.shape[1:]\n",
        "\n",
        "# Define the number of modalities\n",
        "num_modalities = 2\n",
        "\n",
        "# Define the encoder model for each modality\n",
        "encoder_inputs = [Input(shape=input_shape) for _ in range(num_modalities)]\n",
        "encoders = [Dense(256, activati’n='r’lu')(input) for input in encoder_inputs]\n",
        "\n",
        "# Concatenate the encoder outputs to form the latent space representation\n",
        "combined_latent_space = tf.keras.layers.concatenate(encoders)\n",
        "\n",
        "# Define the decoder model for each modality\n",
        "decoder_outputs = [Dense(256, activati’n='r’lu')(combined_latent_space) for _ in range(num_modalities)]\n",
        "\n",
        "# Define the full multimodal autoencoder model\n",
        "autoencoder = Model(inputs=encoder_inputs, outputs=decoder_outputs)\n",
        "\n",
        "# Compile the model\n",
        "autoencoder.compile(optimiz’r='a’am', lo’s='’se')\n",
        "\n",
        "# Train the model\n",
        "autoencoder.fit([dataset] * num_modalities, [dataset] * num_modalities, epochs=10, batch_size=32)\n",
        "\n",
        "# Generate reconstructed outputs for each modality\n",
        "reconstructed_outputs = autoencoder.predict([dataset] * num_modalities)\n"
      ],
      "metadata": {
        "id": "Nz-jBDkVBveF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "from tensorflow.keras.layers import Input, Dense\n",
        "from tensorflow.keras.models import Model\n",
        "from tensorflow.keras.datasets import mnist\n",
        "import numpy as np\n",
        "\n",
        "# Load and preprocess the MNIST dataset\n",
        "(x_train, _), (x_test, _) = mnist.load_data()\n",
        "x_train = x_train.astype('float32') / 255.0\n",
        "x_test = x_test.astype('float32') / 255.0\n",
        "x_train = np.reshape(x_train, (len(x_train), 784))\n",
        "x_test = np.reshape(x_test, (len(x_test), 784))\n",
        "\n",
        "# Define input shape\n",
        "input_shape = (784,)  # MNIST images are 28x28 = 784 pixels\n",
        "\n",
        "# Define number of classes\n",
        "num_classes = 10\n",
        "\n",
        "# Define first encoder model\n",
        "input_images = Input(shape=input_shape)\n",
        "encoder1 = Dense(256, activation='relu')(input_images)\n",
        "latent_space1 = Dense(128, activation='relu')(encoder1)\n",
        "\n",
        "# Define second encoder model\n",
        "encoder2 = Dense(256, activation='relu')(input_images)\n",
        "latent_space2 = Dense(128, activation='relu')(encoder2)\n",
        "\n",
        "# Concatenate the latent spaces from both encoders\n",
        "combined_latent_space = tf.keras.layers.concatenate([latent_space1, latent_space2])\n",
        "\n",
        "# Define decoder model\n",
        "decoder = Dense(256, activation='relu')(combined_latent_space)\n",
        "output_images = Dense(784, activation='sigmoid')(decoder)\n",
        "\n",
        "# Define the full autoencoder model\n",
        "autoencoder = Model(inputs=input_images, outputs=output_images)\n",
        "\n",
        "# Compile the model\n",
        "autoencoder.compile(optimizer='adam', loss='binary_crossentropy')\n",
        "\n",
        "# Train the model\n",
        "autoencoder.fit(x_train, x_train, epochs=10, batch_size=32, validation_data=(x_test, x_test))\n",
        "\n",
        "# Generate reconstructed images\n",
        "reconstructed_images = autoencoder.predict(x_test)\n"
      ],
      "metadata": {
        "id": "A5_oi_r2B9rO"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}